package com.copa.schedule;

import com.copa.module.system.domain.entity.TradeEntity;
import com.copa.module.system.service.TradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author copa
 * @createDate 2021-02-27 12:52
 * @function 每天生成订单数据汇总记录
 */
@Slf4j
@Component
public class TradeSchedule {

    @Autowired
    private TradeService tradeService;

//    @Scheduled(fixedDelay = 10 * 60 * 1000)
    @Scheduled(cron = "0 0 0 * * ?")
    public void insertBill() {
        // 今日新增总订单量
        Long addTotalOrderNum = tradeService.findAddTotalOrderNumByTime();
        // 今日新增小订单量
        Long addOrderGoodsNum = tradeService.findAddOrderGoodsNumByTime();
        // 今日已支付订单
        Long addPayTotalOrderNum = tradeService.findAddPayTotalOrderNumByTime();
        // 今日已支付小订单
        Long addPayOrderGoodsNum = tradeService.findAddPayOrderGoodsNumByTime();
        // 今日已退款小订单数
        Long refundOrderGoodsNum = tradeService.findRefundOrderGoodsNumByTime();
        // 今日盈利
        BigDecimal income = tradeService.findIncomeByTime();
        // 今日退款
        BigDecimal refund = tradeService.findRefundByTime();
        // 今日售出货物总件数
        Long addSaleGoodsNum = tradeService.findAddSaleGoodsNumByTime();
        // 今日退款货物总件数
        Long refundSaleGoodsNum = tradeService.findRefundSaleGoodsNumByTime();

        TradeEntity trade = TradeEntity.builder()
                .addTotalOrderNum(addTotalOrderNum)
                .addOrderGoodsNum(addOrderGoodsNum)
                .addPayTotalOrderNum(addPayTotalOrderNum)
                .addPayOrderGoodsNum(addPayOrderGoodsNum)
                .refundOrderGoodsNum(refundOrderGoodsNum)
                .income(income)
                .refund(refund)
                .addSaleGoodsNum(addSaleGoodsNum)
                .refundSaleGoodsNum(refundSaleGoodsNum)
                .build();
        tradeService.insert(trade);
    }

}
